<template>
  <div class="my-popup" v-show="show">
    <!-- 整体盒子 -->
    <div class="popup-box">
      <!-- 标题 -->
      <div class="popup-title">
        <span>{{ title }}</span>
        <i
          style="cursor: pointer"
          class="el-icon-close"
          @click.stop="$emit('updata:show', false)"
        ></i>
      </div>
      <!-- 搜索框 -->
      <div class="popup-search">
        <el-input
          suffix-icon="el-icon-search"
          :value="value"
          @input="$emit('input', $event)"
        >
        </el-input>
      </div>
      <!-- tree盒子 -->
      <div class="popup-tree">
        <el-tree
          @check="handleCheck"
          :data="data"
          :props="defaultProps"
          show-checkbox
          node-key="id"
          ref="tree"
          :filter-node-method="filterNode"
          :default-checked-keys="defaultChecked"
        ></el-tree>
      </div>
      <!-- 表格盒子 -->
      <div class="popup-table">
        <el-table
          :data="table_data"
          style="width: 100%"
          border
          :filter-node-method="filterNode"
        >
          <el-table-column align="center" type="index" width="30">
          </el-table-column>
          <el-table-column align="center" label="主产品" width="80">
            <template scope="scope">
              <el-radio v-model="radio" :label="scope.row.id">
                &nbsp;
              </el-radio>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="label" label="产品名称">
          </el-table-column>
          <el-table-column align="center" label="操作" width="80">
            <template #default="{ row }">
              <el-button type="text" @click="handleDel(row)" size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 底部栏 -->
      <div class="popup-footer">
        <el-button type="primary" size="mini">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import _ from "loadsh";
export default {
  props: {
    // 弹窗
    show: {
      type: Boolean,
      require: false,
    },
    // 标题
    title: {
      type: String,
      default: "产品",
    },
    // 默认选中
    defaultChecked: {
      type: Array,
      default: () => [],
    },
    // 搜索框
    value: {
      type: [String, Number],
    },
    // 树形结构
    data: {
      type: Array,
      default: () => [
        {
          id: 1,
          label: "一级 1",
          children: [
            {
              id: 5,
              label: "二级 1-1",
              children: [
                {
                  id: 6,
                  label: "三级 1-1-1",
                },
                {
                  id: 7,
                  label: "三级 1-1-2",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          label: "一级 2",
          children: [
            {
              id: 8,
              label: "二级 2-1",
              children: [
                {
                  id: 9,
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              id: 3,
              label: "二级 2-2",
              children: [{ id: 10, label: "三级 2-2-1" }],
            },
          ],
        },
        {
          id: 4,
          label: "一级 3",
          children: [
            {
              id: 11,
              label: "二级 3-1",
              children: [{ id: 12, label: "三级 3-1-1" }],
            },
            {
              id: 13,
              label: "二级 3-2",
              children: [{ id: 14, label: "三级 3-2-1" }],
            },
          ],
        },
      ],
    },
    // 默认节点
    defaultProps: {
      type: Object,
      default: () => ({
        children: "children",
        label: "label",
      }),
    },
  },
  data() {
    return {
      // 表格数据
      table_data: [],
      // 单选框的值
      radio: "",
    };
  },
  methods: {
    // 树形复选款改变时，改变表格
    handleCheck(_, checkNodes) {
      this.table_data = checkNodes.checkedNodes.filter(
        (item) => !item.children
      );
    },
    // 删除按钮事件
    handleDel(row) {
      //#region
      // 如果radio没有选中，则不会删除
      /* if (row.id !== this.radio) {
        this.$message({
          message: "请选择需要删除的产品",
        });
      } else { */
      //#endregion
      // 提示用户是否关闭
      this.$confirm("是否确认删除？")
        .then(() => {
          if (this.radio === row.id) this.radio = "";
          // 将树形的选中状态设为false
          this.$refs.tree.setChecked(row, false);
          // 获取当前选择的index
          const index = _.findIndex(
            this.table_data,
            (item) => item.id === row.id
          );
          // 同步删除表格数据
          this.table_data.splice(index, 1);
        })
        .catch(() => {});
      // }
    },
    // 过滤树形方法
    filterNode(value, data, node) {
      // 过滤搜索方法
      if (!value) return true;
      let { parent } = node;
      let labels = [node.label];
      let level = 1;
      while (level < node.level) {
        labels = [...labels, parent.label];
        parent = parent.parent;
        level++;
      }
      return labels.some((label) => label.indexOf(value) !== -1);
    },
  },
  watch: {
    value: {
      handler(val) {
        this.$refs.tree.filter(val);
      },
    },
  },
  mounted() {
    // 一开始获取默认勾选的值
    this.table_data = this.$refs.tree
      .getCheckedNodes()
      .filter((item) => !item.children);
  },
};
</script>

<style>
.my-popup {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* margin: 0 auto; */
  width: 400px;
}
/* 整体盒子 */
.popup-box {
  border: 1px solid #ececec;
}
/* 标题盒子 */
.popup-title {
  padding: 5px;
  background-color: #f2f2f2;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}
/* 搜索框盒子 */
.popup-search .el-input input {
  height: 25px;
  line-height: 25px;
}
.popup-search .el-input .el-input__suffix {
  top: -7px;
}
/* 插槽盒子 */
.popup-tree {
  height: 200px;
  overflow-y: scroll;
  padding-left: 15px;
}
/* 表格盒子 */
.popup-table {
  height: 200px;
  overflow-y: scroll;
  margin-top: 20px;
}
.popup-table .has-gutter tr th {
  background-color: #f2f2f2;
}
/* 底部盒子 */
.popup-footer {
  display: flex;
  justify-content: flex-end;
  height: 30px;
  background-color: #f2f2f2;
}
</style>
